/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.cv4j.app.drawer;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.utils.Color;
import ohos.app.Context;

import com.cv4j.app.ResourceTable;
import com.lxj.xpopup.core.DrawerPopupView;

import java.util.ArrayList;
import java.util.List;

/**
 * 选择适配器的弹窗
 *
 * @since 2021-05-24
 */
public class DrawerSelectPopup extends DrawerPopupView {
    String curType;
    DrawerSelectPopup.ItemClickedListener itemClickedListener;

    /**
     * 构造函数
     *
     * @param context context
     * @param curType curType
     */
    public DrawerSelectPopup(Context context, String curType) {
        super(context, null);
        this.curType = curType;
    }

    @Override
    protected int getImplLayoutId() {
        return ResourceTable.Layout_select_pop_dialog;
    }

    /**
     * 弹窗初始化
     */
    @Override
    protected void onCreate() {
        super.onCreate();
        ListContainer container = (ListContainer) findComponentById(ResourceTable.Id_listContainer);
        container.setScrollbarColor(new Color(Color.getIntColor("#999999")));
        container.enableScrollBar(Component.VERTICAL, true);
        List<String> list = new ArrayList<>();
        list.add("原图");
        list.add("BoxBlur");
        list.add("Carve");
        list.add("Color");
        list.add("ConBri");
        list.add("Emboss");
        list.add("Exposure");
        list.add("FloSteDithering");
        list.add("Gamma");
        list.add("GaussianBlur");
        list.add("GaussianNoise");
        list.add("Glow");
        list.add("MeansBinary");
        list.add("Mosaic");
        list.add("Nature");
        list.add("SepiaTone");
        list.add("SinCity");
        list.add("Spotlight");
        list.add("Vignette");
        list.add("Water");
        list.add("WhiteImage");
        container.setItemProvider(new DrawerSelectProider(list));
        for (int i1 = 0; i1 <= list.size() - 1; i1++) {
            if (curType.equals(list.get(i1))) {
                container.scrollTo(i1);
            }
        }
        container.setItemClickedListener((listContainer, component, i1, l1) -> {
            if (itemClickedListener != null) {
                ShapeElement element = new ShapeElement();
                element.setRgbColor(RgbColor.fromArgbInt(listContainer.getContext()
                    .getColor(ResourceTable.Color_select_bg)));
                component.setBackground(element);
                itemClickedListener.onClick(list.get(i1));
            }
        });
    }

    public void setItemClickedListener(DrawerSelectPopup.ItemClickedListener itemClickedListener) {
        this.itemClickedListener = itemClickedListener;
    }

    /**
     * 点击接口
     *
     * @since 2021-05-24
     */
    public interface ItemClickedListener {
        /**
         * 点击事件
         *
         * @param menuType menuType
         */
        void onClick(String menuType);
    }
}

